#include "log.h"
#include <stdarg.h>
#define LOG_FILENAME	"runtime_log.txt"

FILE* CLog::pLogFile = NULL; 

void CLog::Write(const char *ptrChar)
{
	if(pLogFile != NULL && ptrChar != NULL)
		fprintf(pLogFile, "%s", ptrChar); 
}

void CLog::Write(string &szText)
{
	if(pLogFile != NULL && szText.length() > 0)
		fprintf(pLogFile, "%s", szText.c_str()); 
}

void CLog::WriteEx(const char* format,...)
{
	char buffer[128] = {0}; 
	va_list	args; 
	va_start (args,format); 
	vsprintf(buffer,format,args); 
	perror(buffer); 
	va_end(args); 
	fprintf(pLogFile, "%s", buffer); 
}

void CLog::WriteLine(char *ptrChar)
{
	if(pLogFile != NULL && ptrChar != NULL)
		fprintf(pLogFile, "%s\n", ptrChar); 
}

void CLog::WriteLine(string &szText)
{
	if(pLogFile != NULL && szText.length() > 0)
		fprintf(pLogFile, "%s\n", szText.c_str()); 
}

void CLog::WriteSpacer()
{
	fprintf(pLogFile, "\n----------------------------------------------\n"); 
}

void CLog::WriteLineEx(const char* format,...)
{
	char buffer[128] = {0}; 
	va_list	args; 
	va_start (args,format); 
	vsprintf(buffer,format,args); 
	perror(buffer); 
	va_end(args); 
	fprintf(pLogFile, "%s\n", buffer); 
}

bool CLog::Close()
{
	if(pLogFile != NULL)
		return (fclose(pLogFile) != EOF); 
	
	return true; 
}

bool CLog::Open()
{
	if(!CLog::Close())
		return false; 

	pLogFile = fopen(LOG_FILENAME, "wt"); 
	return (pLogFile != NULL); 
}
